<?php

namespace Admin\Controller;

use Think\Controller;
use Think\Page;

class GameController extends Controller {

    public function __construct() {
        parent::__construct();
        session("[start]");        
        if (!isset($_SESSION['admininfo']) || $_SESSION['admininfo']['id'] < 1) {
            $this->redirect('Login/index');
        }
    }

    public function index() {
        $gameModel = new \Admin\Model\GameModel();
        $game = $gameModel->select();
        $count = count($game);
        $Page = new Page($count, 10); // 实例化分页类 传入总记录数
        $show = $Page->show(); // 分页显示输出
        $list = $gameModel->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        //var_dump($list);
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }

    /**
     * 添加修改游戏
     */
    public function post() {
        $id = intval($_REQUEST['id']);
        $gameModel = new \Admin\Model\GameModel;
        if ($id) {
            if (IS_POST) {
                $data['gamename'] = I('post.gamename');
                $data['enname'] = I('post.enname');
                $data['cateid'] = I('post.cateid');
                $data['subjectid'] = I('post.subjectid');
                $data['fightid'] = I('post.fightid');
                $data['letterid'] = I('post.letterid');
                $data['orders'] = I('post.orders');
                $data['main_site'] = I('post.main_site');
                $data['bbs_site'] = I('post.bbs_site');
                $data['gold_name'] = I('post.gold_name');
                $data['cny_rate'] = I('post.cny_rate');
                $data['percent'] = I('post.percent');
                $data['descript'] = I('post.descript');
                $data['display'] = I('post.display');
                $data['recommend'] = I('post.recommend');
                $data['recommend_style'] = I('post.recommend_style');
                if ($gameModel->where("id='" . $id . "'")->save($data)) {
                    $this->success("游戏修改成功", "?s=/game/index");
                } else {
                    $this->error("游戏修改失败");
                }
            }
            $game = $gameModel->where("id='" . $id . "'")->find();
            $this->assign('game', $game);
            $this->assign('navname', '修改游戏');
            $this->display();
        } else {
            //添加
            if (IS_POST) {
                $data['gamename'] = I('post.gamename');
                $data['enname'] = I('post.enname');
                $data['cateid'] = I('post.cateid');
                $data['subjectid'] = I('post.subjectid');
                $data['fightid'] = I('post.fightid');
                $data['letterid'] = I('post.letterid');
                $data['orders'] = I('post.orders');
                $data['main_site'] = I('post.main_site');
                $data['bbs_site'] = I('post.bbs_site');
                $data['gold_name'] = I('post.gold_name');
                $data['cny_rate'] = I('post.cny_rate');
                $data['percent'] = I('post.percent');
                $data['descript'] = I('post.descript');
                $data['display'] = I('post.display');
                $data['recommend'] = I('post.recommend');
                $data['recommend_style'] = I('post.recommend_style');
                if ($gameModel->data($data)->add()) {
                    $this->success("游戏添加成功", "?s=/game/index");
                } else {
                    $this->error("游戏添加失败");
                }
            }
            $this->assign('navname', '添加游戏');
            $this->display();
        }
    }
    
    /**
     * 服务器列表
     */
    public function serverlist(){
        $this->display();
    }

}
